<template>
  <div id="app">
    <!-- 登录页面：全屏显示，不显示侧边栏和导航 -->
    <div v-if="isLoginPage" class="login-layout">
      <RouterView />
    </div>

    <!-- 主应用布局：包含侧边栏和导航 -->
    <el-container v-else class="app-container">
      <!-- 侧边栏 -->
      <div class="sidebar">
        <div class="logo">
          <h2>衣柜搭配</h2>
        </div>
        <el-menu
          :default-active="$route.path"
          router
          class="sidebar-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/wardrobe">
            <el-icon><Shirt /></el-icon>
            <span>我的衣柜</span>
          </el-menu-item>
          <el-menu-item index="/outfits">
            <el-icon><Collection /></el-icon>
            <span>搭配管理</span>
          </el-menu-item>
          <el-menu-item index="/recommendation">
            <el-icon><MagicStick /></el-icon>
            <span>智能推荐</span>
          </el-menu-item>
          <el-menu-item index="/analysis">
            <el-icon><DataAnalysis /></el-icon>
            <span>穿搭分析</span>
          </el-menu-item>
          <el-menu-item index="/reminder">
            <el-icon><Bell /></el-icon>
            <span>提醒管理</span>
          </el-menu-item>
          <el-menu-item index="/social">
            <el-icon><Share /></el-icon>
            <span>社交分享</span>
          </el-menu-item>
          <el-menu-item index="/profile">
            <el-icon><User /></el-icon>
            <span>个人设置</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 主内容区 -->
      <div class="main-container">
        <!-- 顶部导航 -->
        <div class="header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ $route.meta.title || '页面' }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <el-dropdown>
              <span class="user-info">
                <el-avatar :size="32" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
                <span class="username">{{ userInfo?.nickname || userInfo?.username || '用户' }}</span>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="goToProfile">个人中心</el-dropdown-item>
                  <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>

        <!-- 主要内容 -->
        <div class="main-content">
          <RouterView />
        </div>
      </div>
    </el-container>
  </div>
</template>

<script setup>
import { RouterView, useRoute, useRouter } from 'vue-router'
import { computed, ref, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()

// 用户信息
const userInfo = ref(null)

// 判断是否为登录页面
const isLoginPage = computed(() => {
  return route.path === '/login' || route.path === '/auth-status'
})

// 加载用户信息
const loadUserInfo = () => {
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    try {
      userInfo.value = JSON.parse(userInfoStr)
    } catch (e) {
      console.error('解析用户信息失败:', e)
    }
  }
}

// 跳转到个人中心
const goToProfile = () => {
  router.push('/profile')
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  userInfo.value = null
  ElMessage.success('已退出登录')
  router.push('/login')
}

// 监听路由变化，重新加载用户信息
watch(route, () => {
  loadUserInfo()
}, { immediate: true })

// 页面初始化
onMounted(() => {
  loadUserInfo()
})
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
}

#app {
  height: 100vh;
  width: 100vw;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
}

/* 登录页面布局 */
.login-layout {
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-container {
  height: 100vh;
  width: 100%;
  display: flex;
}

.sidebar {
  width: 250px;
  min-width: 250px;
  background-color: #304156;
  overflow: hidden;
  flex-shrink: 0;
  height: 100vh;
}

.main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-width: 0;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b2f3a;
}

.logo h2 {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 60px);
  width: 100%;
  overflow-y: auto;
}

.header {
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  width: 100%;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  color: #606266;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
  flex: 1;
  height: calc(100vh - 60px);
  width: 100%;
  box-sizing: border-box;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 200px;
    min-width: 200px;
  }

  .main-content {
    padding: 15px;
  }
}

@media (max-width: 480px) {
  .sidebar {
    width: 180px;
    min-width: 180px;
  }

  .main-content {
    padding: 10px;
  }

  .logo h2 {
    font-size: 16px;
  }
}
</style>
